Tags
Dynamic Menu from Database, Populate Dynamic Menu from Database with recursive function, Populate Dynamic Menu with recursive function
Populate Dynamic Menu from Database with recursive function
Problem: Create a Dynamic Menu in ASP.Net from Database table where data store in Foreign key Relations. (Menu Items can be Nested to ant level)
Table Structure
MenuID | ManuName | MasterID |
---|---|---|
1 | System | 0 |
2 | Menu Creation | 1 |
3 | Role Creation | 1 |
4 | Role Management | 1 |
5 | User Creation | 1 |
6 | User Role Management | 1 |
7 | Email Templates | 1 |
8 | Query Analyzer | 1 |
9 | Master | 0 |
10 | Title Master | 9 |
11 | Type of Office Master | 9 |
12 | Dimension Master | 9 |
13 | Source Type Master | 9 |
14 | Source Details Master | 9 |
15 | Payment Master | 9 |
16 | Business Type Manager | 9 |
17 | Area Master | 9 |
18 | Area KeywordsSynonyms | 9 |
39 | Data Entry | 0 |
40 | Entry | 39 |
41 | Content Management | 0 |
42 | Assignment | 41 |
43 | Validation | 41 |
49 | AASheet Master | 9 |
50 | Locality Update | 41 |
Menu Control in .aspx Page::
<asp:Menu id=”mnuMenu” RunAt=”Server” Orientation=”Horizontal”/>
Code in .cs file
private Dataset MenuDS;
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
//Load Data From DataBase
LoadMenuData();
//Bind Menu Items
BuildMenu(mnuMenu.Items, 0);
}
}
private void LoadMenuData()
{
string strConnection = ConfigurationManager.AppSettings[“ConnectionString”];
string strQuery = “Select MenuId,MenuName,MasterId from MenuMaster”;
SqlConnection conMenu = new SqlConnection(strConnection);
SqlDataAdapter dadMenu = new SqlDataAdapter(strQuery,conMenu);
dadMenu.Fill(MenuDS, “MenuMaster”);
}
private void BuildMenu(MenuItemCollection nodes, Int32 IntParent)
{
Int32 ThisID;
String ThisName;
DataRow[] children =
MenuDS.Tables[0].Select
(“MasterId='” + IntParent + “‘”);
//no child nodes, exit function
if (children.Length == 0) return;
foreach (DataRow child in children)
{
// step 1
ThisID = Convert.ToInt32(child.ItemArray[0]);
// step 2
ThisName = Convert.ToString(child.ItemArray[1]);
// step 3
MenuItem NewNode =
new MenuItem(ThisName, ThisID.ToString());
// step 4
nodes.Add(NewNode);
// step 5
BuildMenu(NewNode.ChildItems, ThisID);
}
}
Menu will populate::
System | Master | Data Entry | Content Management |
Menu Creation | Title Master | Entry | Assignment |
Role Creation | Type of Office Master | Validation | |
Role Management | Dimention Master | Locality Update | |
User Creation | Source Type Master | ||
User Role Management | Source Details Master | ||
Email Template | Payment Master | ||
Query Analyzar | Bussimess Type Master | ||
Area Master | |||
Area Keyword Synonum | |||
AA Sheet Master |
Enjoy!
Rahul Neekhra said:
Hello Jitendra,
First of all thank you.
I have used your code in populating menu from database.
I have got proper solution and this is being used in my company project.
Mohsen said:
Excellent.
great work.amazing.
thanks for this simple but useful way.
you solve all of my problems about dynamic menu.
regards
Mohsen
Nikhita said:
hello Jitendra,
I am using this code. I am not able to make as hyperlink.
Can u explain me.
There is a user table with UserId and UserName, I need to get the Userid as submenu with hyperlink, so that it by click on the hyperlink it should give the corresponding row details.
Thanks,
Nikhita
Nikhita said:
Also, I am able bind in the code. But I not able to display in browser as a dynamic menu..
The column name in the table UserId , User Name.
All the UserId details I need to display as a dynamic menu.
Once I click on UserId 1 ie First menu it should be populated to next page and I need to display the UserId 1 details… ie Userid, username in the next page.
Please send your points to my mail id nikhita.rethinam@wipro.com also give any use links
Muthukumar said:
Good 1…
useful for dynamic menu creation.
Hemali said:
It had helped me a lot.
Thank u…
raju said:
I used your code. It helped me a lot. Thank you so much.
Mrega said:
hey bru. i want to dynamically populate sub-menus for menu items from database from different tables. i’m wondering if you can help me on that?
Daniel said:
This is All Great , i have one question, by the way i’m beginner in c#, why after 4 child nodes i can’t add 5 thanks .
Thanks.
Armando said:
this post is old but its works for me, i have one question??
the level of menu items have limit?